package com.portme.client.pages;

import com.google.gwt.dom.client.Style.Unit;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.user.client.ui.Anchor;
import com.google.gwt.user.client.ui.Composite;
import com.google.gwt.user.client.ui.FlowPanel;
import com.google.gwt.user.client.ui.HTML;
import com.google.gwt.user.client.ui.HasHorizontalAlignment;
import com.google.gwt.user.client.ui.HorizontalPanel;
import com.google.gwt.user.client.ui.Label;
import com.google.gwt.user.client.ui.ResizeLayoutPanel;
import com.google.gwt.user.client.ui.TabLayoutPanel;
import com.portme.client.PortmeServiceClient;
import com.portme.client.StaticFactory;
import com.portme.shared.ClientCustomTab;
import com.portme.shared.LoginInfo;
import com.portme.shared.UserProfile;

public class UserPortfolio extends Composite {
	private FlowPanel holdingPanel = new FlowPanel();
	private String username;
	private LoginInfo currentLogin;
	public UserPortfolio(String username) {
		initWidget(holdingPanel);
		this.username = username;
		currentLogin = StaticFactory.getInstance().getCurrentUserLogin();
		createWidget();
	}
	
	private void createWidget() {
		if(currentLogin != null) {
			HorizontalPanel headerWidget = new HorizontalPanel();
			headerWidget.setHorizontalAlignment(HasHorizontalAlignment.ALIGN_RIGHT);
			
			Label username;
			Anchor login;
			if(currentLogin.isLoggedIn()) {
				login = new Anchor("Logout", currentLogin.getLogoutUrl());
				username = new Label("Welcome "+currentLogin.getUsername());
			} else {
				login = new Anchor("Login", currentLogin.getLoginUrl());
				username = new Label("Have a Google account? ");
			}
			
			headerWidget.add(username);
			headerWidget.add(login);
			
			PortmeServiceClient.getInstance().getUserPortfolio(this.username, new AsyncCallback<UserProfile>() {

				@Override
				public void onFailure(Throwable caught) {
					// TODO Auto-generated method stub
					holdingPanel.add(new HTML("<center><h2>No profile found for "+UserPortfolio.this.username +"</h2></center>"));
				}

				@Override
				public void onSuccess(UserProfile result) {
					TabLayoutPanel tabPanel = new TabLayoutPanel(2.5, Unit.EM);
					// TODO Auto-generated method stub
					for(ClientCustomTab tab : result.getTabs()) {
//			    		tabPanel.add(new SingleTabWidget(tab, null), tab.getTitle());
			    		
			    	}
					tabPanel.setAnimationDuration(1000);
				    
				    ResizeLayoutPanel resizePanel = new ResizeLayoutPanel();
				    resizePanel.setPixelSize(600, 400);
				    resizePanel.setWidget(tabPanel);
					// Return the content
				    if(tabPanel.getWidgetCount() > 0)
				    	tabPanel.selectTab(0);
				    
				    holdingPanel.add(resizePanel);
				}
			});
			
		    
			holdingPanel.add(headerWidget);
			
			
		}
		
	}
}
